package com.sumapay.fsb.gensrc.controller;


import com.alibaba.fastjson.JSON;
import com.sumapay.fsb.common.service.SumaService;
import com.sumapay.fsb.common.util.CommonPage;
import com.sumapay.fsb.common.util.CommonUtil;
import com.sumapay.fsb.gensrc.entity.TradeRecord;
import com.sumapay.fsb.gensrc.service.ITradeRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author suma
 * @since 2021-02-03
 */
@RestController
@RequestMapping("/suma")
public class TradeRecordController {

    @RequestMapping("/qrCodeQuery")
    public String qrCodeQuery(ServletRequest servletRequest) {
        MultiValueMap<String, Object> requestMap = CommonUtil.processRequestParameter(servletRequest);

        return sumaService.qrCodeQuery(requestMap);
    }

    @RequestMapping("/qrCodePay")
    public String qrCodePay(ServletRequest servletRequest) {
        MultiValueMap<String, Object> requestMap = CommonUtil.processRequestParameter(servletRequest);
        Map<String, Object> tradeRecordMap = CommonUtil.paramConvertToSourceMap(servletRequest);
        TradeRecord tt = JSON.parseObject(JSON.toJSONString(tradeRecordMap), TradeRecord.class);
        tt.setTradeStartTime(LocalDateTime.now());
        boolean saveRes = iTradeRecordService.save(tt);
        String result = sumaService.qrCodePay(requestMap);
        tt.setTradeEndTime(LocalDateTime.now());
        boolean updateRes = iTradeRecordService.updateOrgTradeRecord(result, tt);
        return result;
    }

    @RequestMapping("/tradeListQuery")
    public String tradeListQuery(ServletRequest servletRequest) {
        Map<String, Object> tradeRecordMap = CommonUtil.paramConvertToSourceMap(servletRequest);
        CommonPage<TradeRecord> tr = iTradeRecordService.queryList(tradeRecordMap);
        String result = JSON.toJSONString(tr);
        System.out.println(result);
        return result;
    }

    private ITradeRecordService iTradeRecordService;
    private SumaService sumaService;


    @Autowired
    public void setSumaService(SumaService sumaService) {
        this.sumaService = sumaService;
    }

    @Autowired
    public void setITradeRecordService(ITradeRecordService iTradeRecordService) {
        this.iTradeRecordService = iTradeRecordService;
    }
}
